﻿using System;
using Database.Context;
using QuanLyThongTinCoSo.BaseForm;
using QuanLyThongTinCoSo.Biz;
using QuanLyThongTinCoSo.Common;
using Model = Database.Models;

namespace QuanLyThongTinCoSo.Forms.CanBoQuanLy
{
    public partial class frmCanBoQuanLyDetail : frmDetail
    {
        private int canBoId;

        public frmCanBoQuanLyDetail()
        {
            InitializeComponent();
            LoadPhong(0);
            Text = @"Thêm mới cán bộ";

        }

        public frmCanBoQuanLyDetail(int id)
        {
            InitializeComponent();

            Text = @"Sửa thông tin cán bộ";

            _fromtype = Constant.TypeForm.Edit;
            canBoId = id;

            var canBoQuanLyBiz = new CanBoQuanLyBiz();
            var model = canBoQuanLyBiz.GetItemById(id) as Model.CanBo;
            if (model == null) return;

            txtHoTen.Text = model.HoTen;
            txtDienGiai.Text = model.DienGiai;
            txtDienThoai.Text = model.DienThoai;

            if (model.Doi != null)
            {
                int phongId = 0;
                if (model.Doi.Phong != null)
                {
                    phongId = model.Doi.Phong.Id;
                    LoadPhong(model.Doi.Phong.Id);
                }

                LoadDoi(phongId, model.Doi.Id);
            }
        }

        protected override bool Validation()
        {
            bool flag = true;

            errorProvider.Dispose();

            if (string.IsNullOrEmpty(txtHoTen.Text))
            {
                errorProvider.SetError(txtHoTen, "Bạn chưa nhập tên cán bộ. Xin vui lòng kiểm tra lại");
                flag = false;
            }

            return flag;
        }

        protected override void Insert()
        {
            var context = new Context();
            var canBoQuanLyBiz = new CanBoQuanLyBiz(context);
            var doiBiz = new DoiBiz(context);

            var canBo = new Model.CanBo();
            canBo.HoTen = txtHoTen.Text.Trim();
            canBo.DienGiai = txtDienGiai.Text.Trim();
            canBo.DienThoai = txtDienThoai.Text.Trim();

            var doi = drlDoi.SelectedItem as ComboBoxItem;
            if (doi != null && !string.IsNullOrEmpty(doi.Value))
            {
                var doiId = 0;
                if (int.TryParse(doi.Value, out doiId))
                {
                    var doiItem = doiBiz.GetItemById(doiId) as Model.Doi;
                    if (doiItem != null)
                    {
                        canBo.Doi = doiItem;
                    }
                }
            }

            canBoQuanLyBiz.Insert(canBo);
        }

        protected override void Edit()
        {
            var context = new Context();
            var canBoQuanLyBiz = new CanBoQuanLyBiz(context);
            var doiBiz = new DoiBiz(context);

            var model = (Model.CanBo) canBoQuanLyBiz.GetItemById(canBoId);

            model.HoTen = txtHoTen.Text.Trim();
            model.DienGiai = txtDienGiai.Text.Trim();
            model.DienThoai = txtDienThoai.Text.Trim();

            var doi = drlDoi.SelectedItem as ComboBoxItem;
            if (doi != null && !string.IsNullOrEmpty(doi.Value))
            {
                var doiId = 0;
                if (int.TryParse(doi.Value, out doiId))
                {
                    var doiItem = doiBiz.GetItemById(doiId) as Model.Doi;
                    if (doiItem != null)
                    {
                        model.Doi = doiItem;
                    }
                }
            }

            canBoQuanLyBiz.Update(model);
        }

        private void LoadPhong(int phongId)
        {
            drlPhong.Items.Clear();
            int index = 0;
            int select = 0;

            var phongBiz = new PhongBiz();
            foreach (var phong in phongBiz.GetAll())
            {
                var item = new ComboBoxItem(phong.TenPhong, phong.Id.ToString());
                if (phongId == phong.Id)
                {
                    select = index;
                }
                drlPhong.Items.Add(item);

                index++;
            }

            if (drlPhong.Items.Count > 0)
            {
                drlPhong.SelectedIndex = select;
            }
        }

        private void LoadDoi(int phongId, int doiId)
        {
            drlDoi.Items.Clear();

            var context = new Context();
            var doiBiz = new DoiBiz(context);

            int index = 0;
            int select = 0;

            if (phongId == 0)
            {
                foreach (var doi in doiBiz.GetAll())
                {
                    var item = new ComboBoxItem(doi.TenDoi, doi.Id.ToString());
                    if (doiId == doi.Id)
                    {
                        select = index;
                    }
                    drlDoi.Items.Add(item);

                    index++;
                }
            }
            else
            {
                foreach (var doi in doiBiz.GetAllDoiByPhongId(phongId))
                {
                    var item = new ComboBoxItem(doi.TenDoi, doi.Id.ToString());
                    if (doiId == doi.Id)
                    {
                        select = index;
                    }
                    drlDoi.Items.Add(item);

                    index++;
                }
            }

            if (drlDoi.Items.Count > 0)
            {
                drlDoi.SelectedIndex = select;
            }
        }

        protected void PhongChanged(object sender, EventArgs args)
        {
            var phong = drlPhong.SelectedItem as ComboBoxItem;
            if (phong != null && !string.IsNullOrEmpty(phong.Value))
            {
                var phongId = 0;
                if (int.TryParse(phong.Value, out phongId))
                {
                    LoadDoi(phongId, 0);
                }
            }
        }
    }
}